import initServerInstance from '../server/initServerInstance';

if (require.main === module) {
	(async () => {
		const server = await initServerInstance({
			loggerConfig: {
				name: 'upgrade',
				alwaysCreate: true,
			},
		});

		for await (const b of server.app.model.Balance.find()) {
			Object.keys(b.balance.toObject()).forEach(key => {
				const field = b.balance[key];
				if (key === 'sitePortrait') {
					field.charges.forEach(x => {
						Object.assign(x, {
							total: 1,
							limitType: 'limit',
							used: x.used > 0 ? 1 : 0,
						});
					});
				}
				field.total = (field.charges || []).reduce(
					(total, x) => total + x.total,
					0
				);
			});
			await b.save();
		}

		process.exit();
	})();
}
